﻿namespace NServiceBus.AcceptanceTests.Core.Routing;

using System;
using System.Threading.Tasks;
using AcceptanceTesting;
using EndpointTemplates;
using NUnit.Framework;

public class When_sending_non_message_with_routing_configured_by_assembly : NServiceBusAcceptanceTest
{
    [Test]
    public async Task Should_throw_when_sending()
    {
        var context = await Scenario.Define<Context>()
            .WithEndpoint<Endpoint>(b => b.When(async (session, c) =>
            {
                try
                {
                    await session.Send(new NonMessage());
                }
                catch (Exception ex)
                {
                    c.Exception = ex;
                    c.GotTheException = true;
                }
            }))
            .Done(c => c.GotTheException)
            .Run();

        Assert.That(context.Exception.ToString(), Does.Contain("No destination specified for message"));
    }

    public class Context : ScenarioContext
    {
        public bool GotTheException { get; set; }
        public Exception Exception { get; set; }
    }

    public class Endpoint : EndpointConfigurationBuilder
    {
        public Endpoint()
        {
            EndpointSetup<DefaultServer>((c, r) =>
            {
                c.ConfigureRouting().RouteToEndpoint(typeof(NonMessage).Assembly, "Destination");
            });
        }
    }

    public class NonMessage
    {
    }
}